using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
    public DragAndShoot player;
    public ParticleSystem expolsion;
    public Text scoreText;
    public Text liveText;
    public float respawnTime = 3.0f;
    public int lives = 3;
    public float respawnInvulnerabilityTime = 3.0f;
    public int score = 0;
    public GameObject gameOverScreen;
    public AudioSource explosionAudio;


    public void AsteroidDestroyed(Enemy asteroid)
    {
        this.expolsion.transform.position = asteroid.transform.position;
        this.expolsion.Play();
        this.explosionAudio.Play();
        
        
        
        this.score += 25;
        
        scoreText.text = score.ToString();
    }

    public void PlayerDied()
    {
        this.expolsion.transform.position = this.player.transform.position;
        this.expolsion.Play();
        this.lives--;
        explosionAudio.Play();
        liveText.text = lives.ToString();
        if (this.lives == 0)
        {
            GameOver();
        }
        else
        {
            Invoke(nameof(Respawn), respawnTime);
        }
    }

    private void Respawn()
    {
        this.player.transform.position = Vector3.zero;
        this.player.gameObject.layer = LayerMask.NameToLayer("IgnoreCollisions");
        this.player.gameObject.SetActive(true);
        Invoke(nameof(TurnOnCollisions), respawnInvulnerabilityTime);
    }

    private void TurnOnCollisions()
    {
        this.player.gameObject.layer = LayerMask.NameToLayer("Player");
    }
    private void GameOver()
    {
        this.lives = 3;
        this.score = 0;
        gameOverScreen.SetActive(true);
    }
    public void QuitGame()
    {
        Application.Quit();
    }

}
